﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using V5_BLL;
using V5_Model;

namespace V5_EShop.Admin {
    public partial class Admin_HelpList : BasicPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["Action"]);
                switch (Action.ToLower().Trim()) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList() {
            this.MultiViewList.ActiveViewIndex = 0;
            int count = 0;
            this.gvList.DataSource = new BLLHelp().GetList(Pager.PageSize, Pager.CurrentPageIndex, "", ref count);
            this.Pager.RecordCount = count;
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelHelp model = new BLLHelp().GetModel(id);
            if (model != null) {
                this.txtHelpTitle.Text = model.Title;
                this.ddlClassID.SelectedIndex = this.ddlClassID.Items.IndexOf(this.ddlClassID.Items.FindByValue(model.ClassID.ToString()));
                this.txtContent.Text = model.Content;
                this.hfID.Value = model.ID.ToString();

                this.lblTitle.Text = "修改帮助";
                this.btnSave.Text = "修改保存";
            }
        }

        private void Bind_Delete(int id) {
            new BLLHelp().Delete(id);
            this.Bind_gvList();
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_gvList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int id = InputHelper.SetInt(this.hfID.Value);
            string title = this.txtHelpTitle.Text;
            int classid = InputHelper.SetInt(this.ddlClassID.SelectedValue);
            string content = this.txtContent.Text;
            ModelHelp model = new ModelHelp();
            BLLHelp bll = new BLLHelp();
            model.Title = title;
            model.ClassID = classid;
            model.Content = content;
            model.AddDateTime = DateTime.Now;
            model.Hits = 0;
            if (id == 0) {
                model.ID = bll.GetMaxId();
                bll.Add(model);
            }
            else if (id > 0) {
                model.ID = id;
                bll.Update(model);
            }
            this.Bind_gvList();
        }

        protected string Bind_HelpType(object o) {
            int classID = InputHelper.SetInt(o);
            string tempStr = "";
            switch (classID) {
                case 0:
                    tempStr = "未归类";
                    break;
                case 1:
                    tempStr = "新手指南";
                    break;
                case 2:
                    tempStr = "如何付款/退款";
                    break;
                case 3:
                    tempStr = "配送方式";
                    break;
                case 4:
                    tempStr = "售后服务";
                    break;
                case 5:
                    tempStr = "帮助中心";
                    break;
            }
            return tempStr;
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int id = InputHelper.SetInt(e.CommandArgument);
            switch (eName.Trim().ToLower()) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }
    }
}
